import 'package:flutter/material.dart';

import 'package:flutter_ume_kit_mock/data/model/mock.dart';

class MockDetail extends StatelessWidget {
  final Mock mock;
  final void Function() onDismiss;

  const MockDetail(this.mock, {required this.onDismiss, Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onTap: onDismiss,
          child: const ColoredBox(
              color: Colors.black54,
              child: SizedBox(height: 150, width: double.infinity)),
        ),
        Expanded(
          child: ColoredBox(color: Colors.white, child: _mockDetail()),
        ),
      ],
    );
  }

  ///详情
  Widget _mockDetail() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        //返回按钮
        GestureDetector(
          onTap: onDismiss,
          child: const Padding(
            padding: EdgeInsets.all(10.0),
            child: Icon(Icons.arrow_back),
          ),
        ),
        Expanded(
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _mockTitleText(),
                _mockRequestText(),
                _mockDescText(),
                if (mock.fields?.condition != null) _conditionText(),
                if (mock.fields?.delay != null) _delayText(),
                if (mock.fields?.tamper != null) _tamperText(),
                if (mock.fields?.response != null) _mockResponseText()
              ],
            ),
          ),
        )
      ],
    );
  }

  ///标题
  Widget _mockTitleText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16),
      child: Text(
        "${mock.fields?.title}",
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(
            color: Colors.black, fontSize: 16, fontWeight: FontWeight.bold),
      ),
    );
  }

  ///请求
  Widget _mockRequestText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 10),
      child: Row(
        children: [
          Container(
              margin: const EdgeInsets.only(right: 5),
              decoration: BoxDecoration(
                  color: Colors.orange.shade300,
                  borderRadius: const BorderRadius.all(Radius.circular(6))),
              padding: const EdgeInsets.only(left: 6, right: 6, top: 2, bottom: 2),
              child: Text(
                "${mock.request?.fields?.method}",
                style: const TextStyle(
                    color: Colors.white,
                    fontSize: 14,
                    fontWeight: FontWeight.bold),
                strutStyle: const StrutStyle(leading: 0, forceStrutHeight: true),
              )),
          Expanded(
            child: Text(
              mock.request?.fields?.path ?? "",
              style: TextStyle(
                  overflow: TextOverflow.ellipsis,
                  color: Colors.grey.shade700,
                  fontSize: 14,
                  fontWeight: FontWeight.bold),
            ),
          ),
        ],
      ),
    );
  }

  //匹配条件
  Widget _conditionText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "Condition:",
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
          ),
          Text(mock.fields?.condition ?? ""),
        ],
      ),
    );
  }

  //延时
  Widget _delayText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "Delay:",
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
          ),
          Text(mock.fields?.delay.toString() ?? ""),
        ],
      ),
    );
  }

  ///描述
  Widget _mockDescText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, top: 10, right: 16),
      child: Text(
        mock.fields!.desc!,
        style: TextStyle(color: Colors.grey.shade700, fontSize: 14),
      ),
    );
  }

  ///响应
  Widget _tamperText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 10,),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "Tamper:",
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
          ),
          Text(mock.fields?.tamper ?? ""),
        ],
      ),
    );
  }

  ///响应
  Widget _mockResponseText() {
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            "Response:",
            style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
          ),
          Text(mock.fields?.response ?? ""),
        ],
      ),
    );
  }
}
